package com.example.nio.utils;

import cn.hutool.core.map.MapUtil;
import com.example.nio.commons.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;


public class ListWaitUtil {
    /**
     * String   组名
     * List<User>   队列用户
     */

    public static Map<String, List<User>> userMap = new Hashtable<String, List<User>>();
    private static final Logger logger = LoggerFactory.getLogger(ListWaitUtil.class);

    //根据组名用户队列
    public static List<User> getUserList(String department) {
        if(!userMap.containsKey(department)){
            userMap.put(department,new ArrayList<>());
        }
        return userMap.get(department);
    }

    public static Boolean checkUser(String openId) {
        for (String s : userMap.keySet()) {
            for (int i = 0; i < userMap.get(s).size(); i++) {
                if (userMap.get(s).get(i).getOpenId().equals(openId)) {
                    return true;
                }
            }
        }
        return false;
    }

    //添加组
    public static void addDepartment(String department) {
        userMap.put(department, new ArrayList<User>());
    }

    //删除组
    public static void deleteDepartment(String department) {
        userMap.remove(department);
    }

    //添加用户
    public static void add(String department, User user) {
        if (userMap.get(department) == null) {
            addDepartment(department);
        }
        user.setAddTime(new Date());
        for (String users : userMap.keySet()) {
            if (users.equals(department)) {
                List<User> userList = userMap.get(users);
                for (int i = 0; i < userList.size(); i++) {
                    if (userList.get(i).equals(user)) {
                        return;
                    }
                }
                userMap.get(users).add(user);
                //     timer.schedule( new ListRemind(department,user),1*60000);
            }
        }
    }

    //删除用户
    public static void deleteUser(String department, String openId) {
        if(userMap.size() != 0){
            List<User> userList = userMap.get(department);
            userList.removeIf(user -> user.getOpenId().equals(openId));
            for(int i = 0;i < userList.size();i ++){
                if(userList.get(i).getOpenId().equals(openId)){
                    userList.remove(i);
                }
            }
        }
    }

    //根据用户名id返回组名
    public static String getDepartmentById(String openId) {
        if (userMap.size() != 0) {
            for (String users : userMap.keySet()) {
                List<User> userList = userMap.get(users);
                if (userList.size() != 0) {
                    for (User user : userList) {
                        if (user.getOpenId().equals(openId)) {
                            return users;
                        }
                    }
                }
            }
        }
        return null;
    }

    //根据用户名id返回user
    public static User getUserByOpenId(String openId) {
        if (MapUtil.isNotEmpty(userMap)) {
            for (String users : userMap.keySet()) {
                List<User> userList = userMap.get(users);
                for (User user : userList) {
                    if (user.getOpenId().equals(openId)) {
                        return user;
                    }
                }
            }
        }
        return null;
    }

    //根据用户名id返回个数
    public static int getCount(String department, String openId) {
        int count = 0;
        List<User> userList = userMap.get(department);
        for (User user : userList) {
            if (user.getOpenId().equals(openId)) {
                break;
            } else {
                count++;
            }
        }
        return count;
    }

    //队列超时
    public static void listOvertime() {
         Map<String, List<User>> listmap = ListWaitUtil.userMap;
        if (listmap.size() != 0) {
            Date nowdate = new Date();
            for (String department : listmap.keySet()) {
                List<User> list = listmap.get(department);
                List<User> overtimelist = new ArrayList<>();
                for (int lists = 0; lists < list.size(); lists++) {
                    Date addDate = list.get(lists).getAddTime();
                    long subDate = nowdate.getTime() - addDate.getTime();
                    long mins = subDate / 1000 / 60;
                    if (mins % 2 == 0 && mins != 0) {
                        logger.info(list.get(lists).getNickname()+"队列超时"+mins+"分");
                        list.get(lists).setOverTime(mins);
                        overtimelist.add(list.get(lists));
                    }
                }
                if(overtimelist.size() != 0) {
                    System.out.println("我已经等待很久了");
                }
            }
        }
    }
}
